<?php

namespace App\Jobs;

use App\Models\Order;
use App\Service\MiniProgramService;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;

class WaitPayOrder implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $order;
    protected $template;
    protected $formId;

    public function __construct(Order $order, $template, $form_id)
    {
        $this->order = $order;
        $this->template = $template;
        $this->formId = $form_id;
    }


    /**
     *  发送订单超时 消息提醒
     *
     * @return mixed
     */
    public function handle()
    {
        if ($this->order->paid_at) {
            return;
        }
        if(empty($this->formId)) return;
        $data = [
            'keyword1' => $this->order->order_sn,
            'keyword2' => $this->order->productName,
            'keyword3' => $this->order->total_amount,
            'keyword4' => '待支付',
            'keyword5' => $this->order->created_at->toDateTimeString(),
            'keyword6' => '您有一笔订单未支付，超过支付时效将被自动取消，赶快支付吧~',
        ];
        $user = $this->order->user()->first();
        $msg = app(MiniProgramService::class);
        $res = $msg->sendTemplateMsg(
            $user->wx_mini_openid,
            $this->template,
            $this->formId,
            '/pages/orderDetail/orderDetail?order_id=' . $this->order->id,
            $data
        );
        Log::info('支付超时通知结果:', $res);
    }
}
